<template>
  <el-dialog :visible.sync="loginVisible" :before-close="beforeClose" :show-close="!isPage&&showClose"
             :modal="true" :destroy-on-close="true"
             :close-on-press-escape="closeOnPressEscape" :close-on-click-modal="closeOnClickModal"
             width="600px">
    <i class="el-icon-user-solid" slot="title"> 用户登录</i>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder="请输入您的域帐号"
                  suffix-icon="el-icon-user"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" placeholder="请输入您的域密码" type="password" suffix-icon="el-icon-key">></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
     <el-button type="primary" @click="onSubmit" icon="el-icon-check">登录</el-button>
     <el-button @click="doCancle" icon="el-icon-close">取消</el-button>
    </span>
  </el-dialog>

</template>
<script>
  import configuration from '@/Configuration'

  export default {
    name: "login",
    props: {isPage: false},
    data() {
      return {
        loginVisible: true,
        showClose: true,
        closeOnPressEscape: false,
        closeOnClickModal: false,
        form: {}
      }
    },
    mounted: function () {
      console.log(this.$router)
      // if (this.$props.isPage == true) {
      //   const user = this.$store.get("user");
      //   if (user != undefined && user.hasOwnProperty("token")) {
      //     this.$router.replace("/")
      //   }
      // }
    },
    methods: {
      onSubmit: function () {
        this.$ajax.postForm(configuration.LOGIN_URL, this.form).then(response => {
          if (response.success == true && response.SecurityMessage == true) {
            this.$store.set("user", response.data)
            if (this.$props.isPage == true) {
              this.$router.back()
            } else {
              this.loginVisible = false;
            }
          }
        }).catch(error => {
          this.$message.error({
            title: '登录失败',
            message: error.data.result
          });
        })
      },
      doCancle: function () {
        this.loginVisible = false;
      },
      beforeClose: function (done) {
        done();
      }
    }
  }
</script>
<style scoped>
</style>
